// pages/anchor_edit/index.js
import { isPoneAvailable } from '../../utils/reg.js'
import { postedit } from '../../api/live.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:{},
    btncolor:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.state){
      this.setData({state:options.state})
    }
  },
  bindname: function (e) {
    this.setData({
      ['info.announcer.name']: e.detail.value
    })
    this.btncheck()
  },
  bindphone: function (e) {
    this.setData({
      ['info.announcer.phone']: e.detail.value
    })
    this.btncheck()
  },
  bindjob: function (e) {
    this.setData({
      ['info.announcer.post']: e.detail.value
    })
    this.btncheck()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
 toast(title,icon){
    wx.showToast({
      title: title,
      icon: icon,
      duration: 2000
    })
  },
  submit(){
      if(this.data.info.announcer.name==''){
        this.toast('请填写正确的名字','none')
      }else if(!isPoneAvailable(this.data.info.announcer.phone)){
        this.toast('请填写正确的手机号码','none')
      }else if(this.data.info.announcer.post==''){
        this.toast('请填写正确的岗位','none')
      }else if(this.data.info.announcer.name!=''&&this.data.info.announcer.phone!=''&&this.data.info.announcer.post!=''){
        this._postedit()
      }
  },
  _postedit(){
   let {phone,post,name} =this.data.info.announcer
    postedit({phone,post,name}).then(res=>{
      if(res.code==0){
        this.toast(res.msg,'success')
        wx.navigateBack()
      }else{
        this.toast(res.msg,'none')
      }
    })
  },
  btncheck(){
    if(this.data.info.announcer.name!=undefined){
      if(this.data.info.announcer.name!=''&&this.data.info.announcer.phone!=''&&this.data.info.announcer.job!=''){
        this.setData({btncolor:true})
      }else{
        this.setData({btncolor:false})
      }
    }else{
      this.setData({btncolor:false})
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.state=='update'){
      var pages = getCurrentPages();
      var Page = pages[pages.length - 1];//当前页
      var prevPage = pages[pages.length - 2];  //上一个页面
      var date = prevPage.data //取上页data里的数据也可以修改
      this.setData({info:date.info })//设置数据
    }
    this.btncheck()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})